//add by liangyanpeng 15-2-10 下午1:43
/**
 * 点击结算按钮 || 点击提交订单按钮 - 查询库存
 * @returns {undefined}
 */
function checkStock(){
    Ajax.call('flow.php','step=check_stock', checkStockResponse,'GET','JSON');
}

function checkStockResponse(arr){
    //构造数组
    var param = [];
    for(var i=0;i<arr.length;i++){
        if(arr[i].mes !== ''){
            param[i] = arr[i].mes;
            alert(arr[i].mes);
        }
    }
    if(param.length == 0){
        window.location.href="flow.php?step=checkout";
    }else{
        return false;
    } 
}

/**
* 点击加按钮
* @param {type} rec_id
* @param {type} goods_id
* @returns {undefined}
*/
function add_number(rec_id,goods_id)
{
   var number = ++document.getElementById("goods_number_"+rec_id).value;
   Ajax.call('flow.php','step=update_group_cart&rec_id='+rec_id+'&number='+number+'&goods_id='+goods_id,changePriceResponse,'GET','JSON');
}


/**
* 点击减按钮
* @param {type} rec_id
* @param {type} goods_id
* @returns {undefined}
*/
function red_number(rec_id,goods_id)
{
   if(document.getElementById("goods_number_"+rec_id).value>1)
   {
       document.getElementById("goods_number_"+rec_id).value--;
   }
   var number = document.getElementById("goods_number_"+rec_id).value;
   Ajax.call('flow.php','step=update_group_cart&rec_id='+rec_id +'&number='+number+'&goods_id='+goods_id, changePriceResponse,'GET','JSON');
}


function changePriceResponse(res)
{
   if(res.error == 1)
   {
       alert(res.content);
       if(res.rec_id > 0)
       {
           //修改商品数量为当前该商品的库存量
           document.getElementById("goods_number_"+res.rec_id).value = res.number;   
           //更新可结算数目与相应总额，更新单品小计
           document.getElementById("subtotal_"+res.rec_id).innerHTML = res.subtotal;
           //更新可结算商品的总价格和总数量
           if(res.total_settled_price!=null)
           {
                document.getElementById("goods_amount").innerHTML = res.total_settled_price;
                document.getElementById("all_goods_num").innerHTML = res.total_settled_num;
           }
           else
           {
                document.getElementById("goods_amount").innerHTML = "0.00";
                document.getElementById("all_goods_num").innerHTML = "0";
           }
       }
   }
   else
   {
       //更新可结算数目与相应总额，更新单品小计
//       document.getElementById("subtotal_"+res.rec_id).innerHTML = res.subtotal;
//       if(res.total_settled_price!=null)
//       {
//           document.getElementById("goods_amount").innerHTML = res.total_settled_price;
//           document.getElementById("all_goods_num").innerHTML = res.total_settled_num;
//       }
//       else
//       {
//           document.getElementById("goods_amount").innerHTML = "0.00";
//           document.getElementById("all_goods_num").innerHTML = "0";
//       }

        document.getElementById("formCart").innerHTML = res.contentHTML;
   }
}

/**
 * 点击单品复选框【单品是否可被结算】
 * @param {type} rec_id
 * @returns {undefined}
 */
function select_cancel_cart_goods(rec_id)
{
    var ele = document.getElementById("cart_settle_"+rec_id);
    var is_selected = 0;
    if(ele.checked == true)
    {
        is_selected = 1;
    }
    Ajax.call('flow.php', 'step=update_cart_settle&rec_id='+rec_id+"&is_selected="+is_selected,select_cancel_cart_goodsResponse,'GET','JSON');
}

function select_cancel_cart_goodsResponse(arr)
{
    if (arr.error  == 1)
    {
        alert(arr.message);
//        if(arr.rec_id > 0)
//        {
//            //修改商品数量为当前该商品的库存量
//            document.getElementById("goods_number_"+arr.rec_id).value = arr.number;  
//            //更新单品小计
//            document.getElementById("subtotal_"+arr.rec_id).innerHTML = arr.subtotal; 
//        }
    }
    
    //修改全选按钮状态
    if(arr.select_all == 1)
    {
        document.getElementById("selected_all").checked = true;
    }
    else
    {
        document.getElementById("selected_all").checked = false;
    }

    //同步优惠商品与赠品状态
    if(arr.rec_ids)
    {
        //选择优惠商品同时更新赠品状态(选择赠品同时更新优惠商品状态)
        for(var j=0;j<arr.rec_ids.length;j++)
        {
            if(arr['rec_ids'][j])
            {
                if(arr.is_settle == 0)
                {
                    document.getElementById("cart_settle_"+arr['rec_ids'][j]).checked = false;
                }
                else
                {
                    document.getElementById("cart_settle_"+arr['rec_ids'][j]).checked = true;
                }
            }
        }
    }

    if(arr.all_goods_num != 0)
    {
        //修改批量删除按钮状态
        document.getElementById("batBtn").href = "javascript:batchDrop();";
        document.getElementById("batBtn").className = 'razer-bttn'; 
        //修改结算按钮状态
        document.getElementById("subBtn").href = "javascript:checkStock();";
        document.getElementById("subBtn").className = 'razer-bttn';
        //显示已选商品数量和价格总计
        document.getElementById("goods_amount").innerHTML = arr.goods_amount;
        document.getElementById("all_goods_num").innerHTML = arr.all_goods_num;
        // 赠品是否可以加入购物车
        for(var i=0;i<arr['is_available'].length;i++)
        {
            if(arr['is_available'][i]['available'] == 1)
            {
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).className = 'razer-bttn';
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).disabled = false;
            }
            else
            {
                //不可添加赠品至购物车
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).className = 'razer-bttn2 bttn-medium';
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).disabled = true;
            }
        }     
    }else{
            //修改结算按钮状态
            document.getElementById("subBtn").href = "javascript:void(0);";
            document.getElementById("subBtn").className = 'razer-bttn2 bttn-medium';
            //修改批量删除按钮状态
            document.getElementById("batBtn").href = "javascript:void(0);";
            document.getElementById("batBtn").className = 'razer-bttn2 bttn-medium';
            //显示已选商品数量和价格总计
            document.getElementById("goods_amount").innerHTML = "0.00";
            document.getElementById("all_goods_num").innerHTML = "0";
            for(var i=0;i<arr['is_available'].length;i++)
            {
                //不可添加赠品至购物车
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).className = 'razer-bttn2 bttn-medium';
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).disabled = true;
            }
    }
    
}
/**
 * 点击全选复选框【所有是否可被结算】
 * @returns {undefined}
 */
function select_all_goods()
{
    var is_select_all = 0;
    if(document.getElementById("selected_all").checked == true)
    {
        is_select_all = 1;
    }
    Ajax.call('flow.php', 'step=update_all_cart_settle&is_settle='+is_select_all,select_all_goodsResponse, 'GET', 'JSON');
}

function select_all_goodsResponse(arr)
{
    //更新购物车内商品的可结算状态（复选框是否选中）
    var rec_arr = arr.rec_list;
    var is_checked;
    if(arr.is_settle == 1)
    {
        is_checked = true;
    }
    for(var i = 0; i < rec_arr.length; i++)
    {
        document.getElementById("cart_settle_" + rec_arr[i]['rec_id']).checked = is_checked;
    }

    //库存不足时，更新商品数量为当前该商品的库存,并修改该商品价格小计
//    if(arr.goods_number)
//    {
//        for(var p in arr.goods_number)
//        {
//            document.getElementById("goods_number_" + arr.goods_number[p]['rec_id']).value = arr.goods_number[p]['number'];
//            document.getElementById("subtotal_" + arr.goods_number[p]['rec_id']).innerHTML = arr.goods_number[p]['subtotal'];
//        }
//    }

    //更新按钮状态及数据显示
    if(arr.all_goods_num != 0)
    {
        //修改结算按钮状态(购物车只有赠品时不可计算 赠品标识【不可以单纯的以赠品价格来衡量】)
        if(arr.goods_amount == 0)
        {
            document.getElementById("subBtn").href = "javascript:void(0);";
            document.getElementById("subBtn").className = 'razer-bttn2 bttn-medium';
        }
        else
        {
            document.getElementById("subBtn").href = "javascript:checkStock();";
            document.getElementById("subBtn").className = 'razer-bttn';
        }
        //修改批量删除按钮状态
        document.getElementById("batBtn").href = "javascript:batchDrop();";
        document.getElementById("batBtn").className = 'razer-bttn';
        //显示已选商品数量和价格总计
        document.getElementById("goods_amount").innerHTML = arr.goods_amount;
        document.getElementById("all_goods_num").innerHTML = arr.all_goods_num;
        // 赠品是否可以加入购物车
        for(var i=0;i<arr['is_available'].length;i++)
        {
            if(arr['is_available'][i]['available'] == 1)
            {
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).className = 'razer-bttn';
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).disabled = false;
            }
            else
            {
                //不可添加赠品至购物车
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).className = 'razer-bttn2 bttn-medium';
                document.getElementById("addCart_"+arr['is_available'][i]['act_id']).disabled = true;
            }
        }
    }
    else
    {
        //修改结算按钮状态
        document.getElementById("subBtn").href = "javascript:void(0);";
        document.getElementById("subBtn").className = 'razer-bttn2 bttn-medium'; 
        //修改批量删除按钮状态
        document.getElementById("batBtn").href = "javascript:void(0);";
        document.getElementById("batBtn").className = 'razer-bttn2 bttn-medium';
        //显示已选商品数量和价格总计
        document.getElementById("goods_amount").innerHTML = "0.00";
        document.getElementById("all_goods_num").innerHTML = "0";
        // 赠品不可以加入购物车
        for(var i=0;i<arr['is_available'].length;i++)
        {
            document.getElementById("addCart_"+arr['is_available'][i]['act_id']).className = 'razer-bttn2 bttn-medium';
            document.getElementById("addCart_"+arr['is_available'][i]['act_id']).disabled = true;
        }
    }
}

/**
 * 批量删除商品
 * @returns {undefined}
 */
function batchDrop()
{
    var rec_ids = document.getElementsByName('rec_id');
    var value = new Array();
    for(var i = 0; i < rec_ids.length; i++)
    {
        if(rec_ids[i].checked)
        value.push(rec_ids[i].id);
    }  
    Ajax.call('flow.php', 'step=batch_drop&rec_ids='+value,batchDropResponse,'POST','JSON');
}

function batchDropResponse(res)
{
    if(res.error > 0)
    {
        alert(res.msg);
    }
    else
    {
        //重新载入页面
        location.reload();
    }
}
